package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.UserLoginDao;
import com.juyu.miliao.api.dao.redis.UserAuthenticationInfoRedisDao;
import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.dto.UserAuthenticationDTO;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Auther: 卜加超
 * @Date: 2019/5/21 15:02
 * @Description:
 */
@Service
public class MinorModeService {

    @Resource
    private UserRedisDao userRedisDao;
    @Resource
    private UserLoginDao userLoginDao;
    @Resource
    private UserAuthenticationInfoService userAuthenticationInfoService;
    @Resource
    private UserAuthenticationInfoRedisDao userAuthenticationInfoRedisDao;

    public void addMinorModeForPassword(long userId, String password) {
        userRedisDao.addMinorModePassword(userId,password);
        //开启未成年模式
        userLoginDao.updateMinorModeSwitch(userId,2);
        userRedisDao.addMinorMode(userId,2);
    }

    public void checkMinorModePassword(long userId, String password) {

        String minorModePassword = userRedisDao.getMinorModePassword(userId);
        if(password.equals(minorModePassword)){
            //关闭未成年模式
            userLoginDao.updateMinorModeSwitch(userId,1);
            userRedisDao.delMinorMode(userId);
            //删除未成年模式的密码
             userRedisDao.delMinorModePassword(userId);
        }else {
            throw new ConsumeRuntimeException("密码错误，请重新输入");
        }
    }

    /**
     * 发送验证码（未成年模式）
     * @param id
     * @param phone
     */
    public void sendPhoneCode(long id, String phone, CommonHead com) {
        Map<String, String> map = userAuthenticationInfoService.getPhone(id);
        if (map.size() == 0) {

            //没绑定过手机号的逻辑
            //生成验证码
            String verificationCode = (int) ((Math.random() * 9 + 1) * 1000) + "";
            /**
             * 存入redis
             */
            userAuthenticationInfoService.sendPhoneNumValidateCode(id, phone, verificationCode,com);
        } else {
            // 绑定过手机号的逻辑
            //绑定的手机号码
            String tel = map.get("tel");
            if (tel.equals(phone)) {
                //自定义发送验证码
                String verificationCode = (int) ((Math.random() * 9 + 1) * 1000) + "";
                userAuthenticationInfoService.sendPhoneNumValidate(id,phone,verificationCode,com);
            } else {
                throw new ConsumeRuntimeException("手机号与绑定手机号码不一致，请重新输入");
            }

        }
    }

    public void unlockMinorMode(long id, String phone, String code,int isHost) {
        Map<String, String> map = userAuthenticationInfoService.getPhone(id);
        if(map.size()==0){
            //没有绑定手机号走之前绑定手机的逻辑
            UserAuthenticationDTO us = new UserAuthenticationDTO();
            us.setPhone(phone);
            us.setUserId(id);
            us.setCode(code);
            userAuthenticationInfoService.bindingPhone(us,isHost);
            userLoginDao.updateMinorModeSwitch(id,1);
        }else {
            //绑定过手机号的逻辑
             String phoneNumVerificationCode = userAuthenticationInfoRedisDao.getPhoneNumVerificationCode(phone);
            if(StringUtils.isEmpty(code)){
                throw  new ConsumeRuntimeException("验证码过期");
            }
             if(code.equals(phoneNumVerificationCode)){
                 userLoginDao.updateMinorModeSwitch(id,1);
                 userRedisDao.delMinorMode(id);
                 userRedisDao.delMinorModePassword(id);
             }else {
                 throw new ConsumeRuntimeException("验证码不正确,请重新填写");
             }
        }
    }

    public JSONObject getMinorMode(long id) {
         boolean b = userRedisDao.hasKeyMinorMode(id);
         JSONObject jsonObject = new JSONObject();
        if(b){
            String minorMode = userRedisDao.getMinorMode(id);
            jsonObject.put("minorModeSwitch",minorMode);
        }else {
            String s = userLoginDao.selectUserLoginById(id);
            jsonObject.put("minorModeSwitch",s);
        }
        return jsonObject;
    }
}
